{% extends "base.html" %}
{% load i18n %}
{% load bootstrap3 %}
{% load static %}
{% load dv_filters %}
{% block head_title %}{% if session and session.pk %}{% trans "Édition de la session" %}{% else %}{% trans "Nouvelle session" %}{% endif %}{% endblock %}

{% block bootstrap3_extra_head %}{{ form.media.css }}{% endblock %}
{% block bootstrap3_extra_script %}
  <script type="text/javascript" src="{% static 'js/form-changes.js' %}"></script>
{{ form.media.js }}
{% include 'snippets/datetimepicker_js.html' %}
<script type="text/javascript">
function updateHelpersTime() {
  var begin_time = $( "#id_begin" ).val();
  var helpers_time = $( "#id_helpers_time" ).val();
  if ( begin_time != '') {
      begin_time_parsed = begin_time.split(":");
      if (typeof begin_time_parsed[1] === 'undefined') {
          begin_time_parsed[1] = 0;
      }
      // 0 left-pad the numbers
      helpers_time = ("00" + (begin_time_parsed[0] - 1)).slice(-2) + ':' + ("00" + begin_time_parsed[1]).slice(-2);

      $( "#id_helpers_time" ).val(helpers_time);
  }
}

function updateHelpersPlace() {
  var place = $( "#id_place" ).val();
  var helpers_place = $( "#id_helpers_place" ).val();
  if ( place != '') {
      $( "#id_helpers_place" ).val(place);
  }
}

$( "#id_begin" ).change( updateHelpersTime );
$( "#id_place" ).change( updateHelpersPlace );
</script>
{% endblock %}

{% block content %}
    <h1>{% if session and session.pk %}{% trans "Édition de la session" %}{% else %}{% trans "Nouvelle session" %}{% endif %}</h1>

    <form method="POST" class="form-horizontal" id="form-session" data-prevent-page-exit="" action="{{ action }}">
        {% csrf_token %}
        <h3>{% trans "Emplacement" %}</h3>
        {% bootstrap_field form.orga layout='horizontal' size='small' %}
        {% bootstrap_field form.day layout='horizontal' size='small' %}
        {% bootstrap_field form.begin layout='horizontal' size='small' %}
        {% bootstrap_field form.place layout='horizontal' size='small' %}
        {% include "address_bootstrap_form.html" with has_additional=True %}
        {% bootstrap_field form.fallback_plan layout='horizontal' size='small' %}
        {% if form.visible %}
          <h3>{% trans "Logistique" %}</h3>
          <div class="form-group form-group-sm">
            {% bootstrap_label form.visible.label label_class='col-md-3 control-label' %}
            <div class="col-md-1">
                <div class="checkbox">
                    <label for="id_visible">
                        <input {% if form.visible.value %}checked="checked" {% endif %}
                              {% if form.visible.field.disabled %}disabled {% endif %}
                              id="id_visible" name="visible" type="checkbox" />
                    </label>
                </div>
            </div>
          </div>
          {% bootstrap_field form.superleader layout='horizontal' size='small' %}
          {% bootstrap_field form.bikes_concept layout='horizontal' size='small' %}
          {% bootstrap_field form.bikes_phone layout='horizontal' size='small' %}
          {% bootstrap_field form.apples layout='horizontal' size='small' %}
          {% bootstrap_field form.helpers_time layout='horizontal' size='small' %}
          {% bootstrap_field form.helpers_place layout='horizontal' size='small' %}
          {% bootstrap_field form.comments layout='horizontal' size='small' %}
        {% endif %}
        {% buttons %}
            <button type="submit" class="btn btn-primary pull-right">{% trans "Enregistrer" %}</button>
            {% if session and session.pk %}
                <a class="btn btn-default" href="{% url 'session-detail' seasonpk=season.pk pk=session.pk %}">{% trans "Annuler" %}</a>
                {% if user|can:'challenge_session_crud' %}
                  <a class="btn btn-default" title="{% trans "Dupliquer" %}" href="{% url 'session-clone' seasonpk=season.pk pk=session.pk %}"><span class="glyphicon glyphicon-copy"></span></a>
                {% endif %}
            {% elif season.pk %}
                <a class="btn btn-default" href="{% url 'season-detail' season.pk %}">{% trans "Annuler" %}</a>
            {% endif %}
        {% endbuttons %}
    </form>
{% endblock %}
